package go_logger

import (
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	lumberjackV2 "gopkg.in/natefinch/lumberjack.v2"
	"os"
)

var (
	Logger   *zap.Logger
	LogLevel = zapcore.DebugLevel
)

func LogInit() {
	writeSyncer := getLogWriter()
	encoder := getEncoder()
	core := zapcore.NewCore(encoder, writeSyncer, LogLevel)

	Logger = zap.New(core, zap.AddCaller())
}

func getEncoder() zapcore.Encoder {
	encoderConfig := zap.NewProductionEncoderConfig()
	encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
	//encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
	encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
	return zapcore.NewConsoleEncoder(encoderConfig)
}

func getLogWriter() zapcore.WriteSyncer {
	lumberjack := &lumberjackV2.Logger{
		Filename:   "./logger.log", // 文件名
		MaxSize:    10,             // log文件最大MB
		MaxBackups: 10,             // log文件最大备份数
		MaxAge:     30,             // 最长保存时间
		Compress:   false,          // 是否压缩
	}

	//return zapcore.AddSync(lumberjack)
	return zapcore.NewMultiWriteSyncer(os.Stdout, zapcore.AddSync(lumberjack))
}
